package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.news.ProblemInformationActivity;
import com.renwei.yunlong.activity.work.WorkInformationActivity;
import com.renwei.yunlong.adapter.KnowRealtedAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.KnowRelatedBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangqi on 2018/4/17.
 */

public class KnowRelatedFragment extends BaseLazyFragment implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private String knowledgeId;
    private KnowRealtedAdapter adapter;

    public KnowRelatedFragment(String knowledgeId) {
        this.knowledgeId = knowledgeId;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_know_relate;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    private void initView() {
        adapter = new KnowRealtedAdapter(getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(this);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("knowledgeId", knowledgeId);
        ServiceRequestManager.getManager().getKnowRelate(getContext(), JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onItemClick(View view, int position) {
        KnowRelatedBean.RowsBean bean = adapter.getItem(position);
        if (bean != null) {
            if ("0".equals(bean.getType())) {
                WorkInformationActivity.OpenActivity(getContext(), bean.getRelationId() + "", bean.getServerRequestId(), bean.getOutsourceFlag());
            } else if ("1".equals(bean.getType())) {
                ProblemInformationActivity.OpenActivity(getContext(), bean.getRequestId(), bean.getRelationId() + "", bean.getOutsourceFlag());
            }
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                KnowRelatedBean bean = new Gson().fromJson(result, KnowRelatedBean.class);
                if (bean.getMessage().getCode() == 200) {
                    adapter.setData(bean.getRows());
                    if (bean.getRows().size() == 0) {
                        stateLayout.showEmptyView();
                    } else {
                        stateLayout.showContentView();
                    }
                }
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
